package com.hp.ssmp.util;

import com.hp.ssmp.entity.TUser;

import java.io.Serializable;

/**
 * @Author: wanjianhong
 * @Version: 1.0
 * @Description:
 * 统一响应体
 */
public class R<T> implements Serializable {

    private boolean flag;//是否成功
    private Integer code;//返回码
    private String message;//返回消息
    private String imgUrl;//图片地址


    private T data;//返回数据

    public R(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T)data;
    }

    public R(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public R() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "执行成功";
    }

    @Override
    public String toString() {
        return "R{" +
                "flag=" + flag +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", imgUrl='" + imgUrl + '\'' +
                ", data=" + data +
                '}';
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static R  success(){
        return new R();
    }

    public static <T> R<T>  success(T t){
        return new R<T>(true, StatusCode.OK,"操作成功", t);
    }

    public static R error(){
        return new R(false, StatusCode.ERROR,"操作失败");
    }

    public static R error(Integer code, String message){
        return new R(false, code,message);
    }
}